﻿using AutoMapper;
using Common.Communal;
using ERP.Domain.System;
using MediatR;
using System.Api.Write.Applocations.Commands.Posts;
using System.Services.Posts;

namespace System.Api.Write.Applocations.CommandHandlers.Posts
{
    /// <summary>
    /// 更新岗位命令处理程序
    /// </summary>
    public class PostUpdateCommandHandler : IRequestHandler<PostUpdateCommand, ApiResult<int>>
    {
        /// <summary>
        /// 映射
        /// </summary>
        private readonly IMapper mapper;
        /// <summary>
        /// 岗位服务
        /// </summary>
        private readonly IPostService service;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mapper">映射</param>
        /// <param name="service">岗位服务</param>
        public PostUpdateCommandHandler(IMapper mapper, IPostService service)
        {
            this.mapper = mapper;
            this.service = service;
        }
        /// <summary>
        /// 处理更新岗位命令
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">取消 </param>
        /// <returns>返回任务</returns>
        public async Task<ApiResult<int>> Handle(PostUpdateCommand request, CancellationToken cancellationToken)
        {
            var res = mapper.Map<Post>(request);
            return await service.Update(res);
        }
    }
}
